package cn.com.connected.service.impl;

import cn.com.connected.common.config.CurrentData;
import cn.com.connected.common.emnus.StatusType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import cn.com.connected.service.SysPublicService;
import static cn.com.connected.entity.SysPublicEntity.*;
import cn.com.connected.base.params.page.BaseSearchDto;
import cn.com.connected.base.params.page.PageParam;
import cn.com.connected.base.params.page.PageResult;
import cn.com.connected.base.utils.BeanCopyUtils;
import cn.com.connected.dto.SysPublicDto;
import cn.com.connected.dto.search.SysPublicSearchDto;
import cn.com.connected.entity.SysPublicEntity;
import cn.com.connected.mapper.SysPublicMapper;
import cn.com.connected.vo.SysPublicVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 公告
 *
 * @author sms
 * @email sms@live.com
 * @date 2022-10-31 14:09:14
 */
@Slf4j
@Service("sysPublicService")
public class SysPublicServiceImpl implements SysPublicService {
    @Resource
    private SysPublicMapper sysPublicMapper;

    @Override
    public SysPublicVo sysPublicAdd(SysPublicDto sysPublicDto) {
        SysPublicEntity entity = new SysPublicEntity();
        BeanCopyUtils.copy(sysPublicDto,entity);
        sysPublicMapper.insert(entity);

        return sysPublicInfo(entity.getId());
    }

    @Override
    public void sysPublicEdit(String id, SysPublicDto sysPublicDto) {
        SysPublicEntity entity = sysPublicMapper.selectById(id);
        BeanCopyUtils.copy(sysPublicDto,entity);
        sysPublicMapper.updateById(entity);
    }

    @Override
    public SysPublicVo sysPublicInfo(String id) {
        SysPublicEntity entity = sysPublicMapper.selectById(id);
        return BeanCopyUtils.copy(entity,new SysPublicVo());
    }

    @Override
    public void sysPublicDel(String id) {
        SysPublicEntity entity = sysPublicMapper.selectById(id);
        entity.setStatus(StatusType.t0.getValue());
        sysPublicMapper.updateById(entity);
    }

    @Override
    public PageResult<SysPublicVo> sysPublicPage(BaseSearchDto<SysPublicSearchDto> searchParams) {
        LambdaQueryWrapper<SysPublicEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysPublicEntity::getStatus,StatusType.t1.getValue());

        PageParam pageParam = searchParams.getPageParam();
        PageHelper.startPage(pageParam.getPageIndex(), pageParam.getPageSize());
        SysPublicSearchDto searchDto = searchParams.getSearchOption();
        if(searchDto.getPublicType()!=null){
            wrapper.eq(SysPublicEntity::getPublicType,searchDto.getPublicType());
        }

        wrapper.orderByDesc(Arrays.asList(SysPublicEntity::getSort,SysPublicEntity::getCreateTime));

        Page<SysPublicEntity> page = (Page)sysPublicMapper.selectList(wrapper);

        PageResult<SysPublicVo> pageResult = BeanCopyUtils.copyPageResult(page, SysPublicVo.class);

        return pageResult;
    }

}
